package com.binary.coder.gift.core.config.schedule;

import io.netty.util.concurrent.DefaultThreadFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author qinqin.luo.
 * @title
 * @time 2023/2/20.
 */
@Configuration
@EnableScheduling
@Slf4j
public class ScheduleConfig implements SchedulingConfigurer {

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.setScheduler(taskExecutor());
    }

    @Value("${scheduled.max.core:10}")
    Integer scheduledMaxCoreSize;


    @Bean("scheduledExecutor")
    public ScheduledExecutorService taskExecutor() {
        return new ScheduledThreadPoolExecutor(scheduledMaxCoreSize,new DefaultThreadFactory("scheduled-"),new ThreadPoolExecutor.CallerRunsPolicy());
    }
}
